package com.wjh.kt_activity.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wjh.client.user.UserInfoFeignClient;
import com.wjh.kt_vod.model.activity.CouponUse;
import com.wjh.kt_activity.mapper.CouponUseMapper;
import com.wjh.kt_activity.service.CouponUseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wjh.kt_vod.model.user.UserInfo;
import com.wjh.kt_vod.vo.activity.CouponUseQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 优惠券领用表 服务实现类
 * </p>
 *
 * @author Wang_Junhong
 * @since 2022-07-16
 */
@Service
public class CouponUseServiceImpl extends ServiceImpl<CouponUseMapper, CouponUse> implements CouponUseService {

    @Autowired
    private UserInfoFeignClient userInfoFeignClient;

    @Override
    public IPage<CouponUse> getCouponUseInfoPage(Page<CouponUse> couponUsePage, CouponUseQueryVo couponUseQueryVo) {

        Long couponId = couponUseQueryVo.getCouponId();
        String couponStatus = couponUseQueryVo.getCouponStatus();
        String getTimeBegin = couponUseQueryVo.getGetTimeBegin();
        String getTimeEnd = couponUseQueryVo.getGetTimeEnd();

        LambdaQueryWrapper<CouponUse> couponUseLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(couponId)) {
            couponUseLambdaQueryWrapper.eq(CouponUse::getCouponId, couponId);
        }
        if (!StringUtils.isEmpty(couponStatus)) {
            couponUseLambdaQueryWrapper.eq(CouponUse::getCouponStatus, couponStatus);
        }
        if (!StringUtils.isEmpty(getTimeBegin)) {
            couponUseLambdaQueryWrapper.ge(CouponUse::getGetTime, getTimeBegin);
        }
        if (!StringUtils.isEmpty(couponId)) {
            couponUseLambdaQueryWrapper.le(CouponUse::getGetTime, getTimeEnd);
        }
        Page<CouponUse> page = baseMapper.selectPage(couponUsePage, couponUseLambdaQueryWrapper);
        List<CouponUse> records = page.getRecords();
        records.stream().forEach(couponUse->{

            Long userId = couponUse.getUserId();

            UserInfo inner = userInfoFeignClient.inner(userId);
            if (!StringUtils.isEmpty(inner)){
                couponUse.getParam().put("nickName",inner.getNickName());
                couponUse.getParam().put("photo",inner.getPhone());
            }

        });
        return page;
    }



}
